<?php
	class Permiss extends MY_Controller {
		function Permiss() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();
			$_SESSION['redirect'] = $this->uri->uri_string();		
		}
		
		function index(){	
			$data['title'] = "Quản lý quyền";
			$data['table'] = 'permiss_admins';
			$data['controller'] = 'permiss';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin'] = 'back_end/permiss/index';
			
			$per_page = 20;
			$uri = 4;
			$data['admins_value'] = $this->mod_lib->load_all('permiss_admins','',array(),$per_page,$this->uri->segment($uri),array("id"=>"desc"));																													
			$config['base_url']     =  site_url('back_end/permiss/index/');
			$config['total_rows']   =  $this->db->count_all('permiss_admins');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);		
		}
		
		/**
		 * @desc: Phan quuyen su dung cho nguoi dung he thong
		 */
		function permission($id){
			$data['title'] 			= " Phân quyền cho thành viên quản trị";
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin']  = 'back_end/admins/index';	
			$this->db->where('id',$id);
			$data['admin']          = $this->db->get('admins')->row_array();
			$data['admin_id']		= $id;
			$data['admin_user']		= $data['admin']['username'];
			$data['content_admin']	= 'back_end/admins/permission';
			$data['menu']			= $this->mod_lib->permis_menu_admin($id);
		
			if($this->input->post('add'))
			{
				foreach ($data['menu'] as $menu)
				{
					$permis_admin_id = $menu['permis_admin_id'];
					$menu_admin_id 	 = $menu['menu_admin_id'];
					$Per 	= 0;
					$Post 	= 0;
					$Edit 	= 0;
					$Del 	= 0;
					
					if($this->input->post("Per_".$permis_admin_id) == 1)
					{
						$Per 		= $this->input->post("Per_".$permis_admin_id); 
						if($this->input->post("Post_".$permis_admin_id)==1) 
						{
							$Post 	= $this->input->post("Post_".$permis_admin_id);		
						}
						if($this->input->post("Edit_".$permis_admin_id)==1)
						{
							$Edit 	= $this->input->post("Edit_".$permis_admin_id);		
						}
						if($this->input->post("Del_".$permis_admin_id)==1)
						{
							$Del 	= $this->input->post("Del_".$permis_admin_id);	
						}
					}
					
					$sql_update = "UPDATE aha_permis_admin SET access = '$Per', additional = '$Post', edit = '$Edit', del = '$Del' WHERE admin_id = '$id' AND id = '$permis_admin_id' AND menu_admin_id = '$menu_admin_id'";	
					mysql_query($sql_update);
				}
				
				redirect('back_end/admins/permission/'.$id);
			}
			
			/*
			$data['admin_id']		= $id;
			$data['admin_user']		= $user;
			$data['content_admin']	= 'back_end/admins/permis_admin';
			*/
			$this->load->view('back_end/template_backend',$data);
		}
		
		function add(){	
			$data['title'] = "Thêm thành viên quản trị";
			$data['table'] = 'admins';
			$data['controller'] = 'admins';
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			$data['content_admin'] = 'back_end/admins/add';
			$add = $this->input->post('addform');
			
			if(!empty($add)){ 
				$data_value = array (
						'username'       => trim($this->input->post('username')),
						'fullname'       => trim($this->input->post('fullname')),
						'password'       => md5(trim($this->input->post('password'))),  
						'status'         => trim($this->input->post('status')),
						'email'          => trim($this->input->post('email')) 				
					);

				if($this->mod_lib->insert('admins',$data_value)) {
					$this->session->set_flashdata('msview', 'Thêm thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Thêm thất bại !');
				}
				
				redirect('back_end/admins');	
			} else {
				$this->load->view('back_end/template_backend',$data);
			}		
		}
		
		function edit($id = null){
			$data['title']			= "Sửa thông tin quản trị viên";
			$data['content_admin'] = 'back_end/admins/edit';
			$data['admins_value'] = $this->db->get_where('admins',array( 'id' => $id ))->result_array();
			$edit = $this->input->post('editForm');
			$this->db->order_by('parent_id','asc');
			$data['category_admin'] = $this->db->get('admin_menu')->result_array();
			
			if(!empty($edit)) {
				$where = array(
			  			'id' => $id 
			  		);
			  	$order = array(
			  			'id' => 'DESC'
			  		);		
				$data_value = array(
						'username' => trim($this->input->post('username')),
						'fullname' => trim($this->input->post('fullname')),
						'email' => trim($this->input->post('email')),
						'status' => trim($this->input->post('status')),
						'password' => trim(md5($this->input->post('password')))
					);
					
				$this->mod_lib->update('admins',$where,$order,$data_value);
				
				redirect('back_end/admins');
			} else {
				$this->load->view('back_end/template_backend',$data);	
			}
		}
		 
		function delete(){
			$value_id_arr    = $this->input->post('value_id');
			
			if(!empty($value_id_arr)){
				$value_id_string = implode(',',$value_id_arr);
				
				if($this->mod_lib->deleteRecord('admins',$value_id_string)){
					$this->session->set_flashdata('msview', 'Xóa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Xóa thất bại !');
				}
				
				$this->session->set_flashdata('msview', 'Xóa thành công !');
			} else {
				$this->session->set_flashdata('msview', 'Xóa thất bại !');
			}
			
			redirect('back_end/admins');
		}
	}
?>